package com.javaear.beangenerator.tools;

import java.io.*;

/**
 * this is for write the Beaner String to a File
 * or get code template from path
 *
 * @author aooer
 */
public abstract class IOUtils {

    /**
     * write Beaner to file
     *
     * @param sourceStr    beaner.toString
     * @param destFilePath destPath
     * throws IOException
     */
    public static void write(String sourceStr, String destFilePath) throws IOException {
        File destFile = new File(destFilePath);
        if (!destFile.getParentFile().exists() && destFile.getParentFile().mkdirs())
            System.out.println("create dest dir success");
        BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));
        writer.write(sourceStr);
        writer.flush();
        writer.close();
    }

    /**
     * get content from path
     *
     * @param sourceFile sourceFilePath
     * @return source file content
     * throws IOException
     */
    public static String getContent(String sourceFile) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(new File(sourceFile)));
        String temp;
        StringBuilder contentBuilder = new StringBuilder(temp = reader.readLine());
        while ((temp = reader.readLine()) != null)
            contentBuilder.append(temp).append("\r\n");
        reader.close();
        return contentBuilder.toString();
    }
}
